package com.cyy.utils;

import net.masterthought.cucumber.Configuration;
import net.masterthought.cucumber.ReportBuilder;
import net.masterthought.cucumber.Reportable;
import net.masterthought.cucumber.json.support.Status;
import net.masterthought.cucumber.presentation.PresentationMode;

import java.io.File;
import java.util.*;

public class PrettyReport {

    /**
     * 生成美化的测试报告
     * @param reportOutputDirectory 输出报告的文件路径
     * @param projectName 项目的名字
     * @param jsonFileMap 要解析的json文件路劲,格式: <div>path:qualifier</div>
     * @param classifications 报告的相关信息，会在报告的右上角展示,格式: name:value
     * @return
     */
    public static Reportable report(String reportOutputDirectory, String projectName,Map<String, String> jsonFileMap, Map<String, String> classifications) {
        File reportOutputDirectoryObj = new File(reportOutputDirectory);
        String buildNumber = "1";
        Configuration configuration = new Configuration(reportOutputDirectoryObj, projectName);

        // optional configuration - check javadoc for details
        configuration.addPresentationModes(PresentationMode.RUN_WITH_JENKINS);
        // do not make scenario failed when step has status SKIPPED
        configuration.setNotFailingStatuses(Collections.singleton(Status.SKIPPED));
        configuration.setBuildNumber(buildNumber);
        // addidtional metadata presented on main page
        classifications.keySet().forEach(name ->{
            configuration.addClassifications(name, classifications.get(name));
        });

        // optionally specify qualifiers for each of the report json files
        configuration.addPresentationModes(PresentationMode.PARALLEL_TESTING);
        List<String> jsonFiles = new ArrayList<>();
        jsonFileMap.keySet().forEach(jsonFileName -> {
            String[] split = jsonFileName.replace(".json","").split("\\\\");
            configuration.setQualifier(split[split.length-1],
                    jsonFileMap.get(jsonFileName));
            jsonFiles.add(jsonFileName);
        });
        ReportBuilder reportBuilder = new ReportBuilder(jsonFiles, configuration);
        Reportable result = reportBuilder.generateReports();
        return result;
    }

    public static Reportable report(Map<String, String> jsonFileMap, Map<String, String> classifications) {
        return report("report/pretty","车友援", jsonFileMap, classifications);
    }


    public static void main(String[] args) {
        String prjPath = System.getProperty("user.dir");
        Map<String, String> jsonFileMap = new HashMap<>();
        jsonFileMap.put(prjPath+"/report/cucumber-reports/app-2020-10-29 110146.json","amigo");
//        jsonFileMap.put(prjPath+"/report/cucumber-reports/cucumber-json-oppo.json","oppo");
        // 相关信息
        Map<String, String> classifications = new HashMap<>();
        classifications.put("oppo-cpu","8G");
        classifications.put("amigo-cpu","4G");
        PrettyReport.report(jsonFileMap, classifications);
    }

}
